package com.caifu.util.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {

    public static final String DATE_PATTERN_YYYYMMDDHHMMSS_01 = "yyyyMMddHHmmss";
    public static final String DATE_PATTERN_YYYYMMDDHHMMSS_02 = "yyyy-MM-dd HH:mm:ss";
    public static final String TIME_PATTERN_HHMMSS_01 = "HHmmss";
    public static final String TIME_PATTERN_HHMMSS_02 = "HH:mm:ss";
    public static final String DATE_PATTERN_YYYYMMDD_01 = "yyyyMMdd";
    public static final String DATE_PATTERN_YYYYMMDD_02 = "yyyy-MM-dd";


    /**
     * 格式时间中文化 (例：2017年1月1日)
     *
     * @param date
     * @return
     */
    public static String formatChineseTime(String date) {
        if (date == null) {
            return null;
        } else if (date.length() == 4) {
            date = Integer.parseInt(date.substring(0, 2)) + "月" + Integer.parseInt(date.substring(2, 4)) + "日";
        } else if (date.length() == 8) {
            date = date.substring(0, 4) + "年" + Integer.parseInt(date.substring(4, 6)) + "月" + Integer.parseInt(date.substring(6, 8)) + "日";
        }
        return date;
    }

    /**
     * 计算当前日期是星期几
     *
     * @param date yyyyMMdd
     * @return 周日1 周一2 周二3 周三4 周四 5 周五6 周六7
     * @throws Exception
     */
    public static int dayForWeek(String date) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar c = Calendar.getInstance();
        c.setTime(format.parse(date));
        return c.get(Calendar.DAY_OF_WEEK);

    }

    //根据当前日期计算星期数
    public static int getWeekByDate(Date date) {
        int week = 0;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int week_index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index == 0) {
            week = 7;
        } else {
            week = week_index;
        }
        return week;
    }

    public static String getDateTimeByNumber(int number) {
        String date = "";
        Calendar calendar = Calendar.getInstance();
        for (int i = 1; i <= 7; i++) {
            calendar.setTime(getFetureDate(i));
            int week_index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String result = format.format(getFetureDate(i));
            if (number == 7 && week_index == 0) {
                date = result;
            } else {
                if (number == week_index) {
                    date = result;
                }
            }
        }
        return date;
    }

    //计算当前日期后几天的日期    返回值yyyymmdd
    public static Date getFetureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        return today;
    }

    //获取当前日期之后的日期  MM月DD日
    public static String getDateByNumber(int number) {
        String date = "";
        Calendar calendar = Calendar.getInstance();
        for (int i = 1; i <= 7; i++) {
            calendar.setTime(getFetureDate(i));
            int week_index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String[] result = format.format(getFetureDate(i)).split("-");
            if (number == 7 && week_index == 0) {
                date = result[1] + "月" + result[2] + "日";
            } else {
                if (number == week_index) {
                    date = result[1] + "月" + result[2] + "日";
                }
            }
        }
        return date;
    }

    //根据1234567返回星期几
    public static String getWeekByNumber(int number) {
        String week = "";
        switch (number) {
            case 1:
                week = "周一";
                break;
            case 2:
                week = "周二";
                break;
            case 3:
                week = "周三";
                break;
            case 4:
                week = "周四";
                break;
            case 5:
                week = "周五";
                break;
            case 6:
                week = "周六";
                break;
            case 7:
                week = "周日";
                break;
        }
        return week;
    }

    //日期延后7天，并去除非工作日
    //appintDate 后延多长时间
    public static List<String> appointDate(int appintDate) {
        List<String> dateList = new ArrayList<String>();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int i = 0;
        while (i < appintDate) {
            calendar.add(Calendar.DAY_OF_YEAR, -1);
            if ((calendar.get(Calendar.DAY_OF_WEEK)) != 1 && (calendar.get(Calendar.DAY_OF_WEEK)) != 7) {
                Date date1 = calendar.getTime();
                dateList.add(sdf.format(date1));
                i++;
            }
        }
        return dateList;
    }


    /**
     * 算时间差
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param type      1-差多少天  2-差多少小时
     * @return
     * @throws ParseException
     */
    public static long dateDiff(String startTime, String endTime, String type) throws ParseException {
        //按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat("yyyyMMddHHmmss");
        long nd = 1000 * 24 * 60 * 60;//一天的毫秒数
        long nh = 1000 * 60 * 60;//一小时的毫秒数
        //long nm = 1000*60;//一分钟的毫秒数
        //long ns = 1000;//一秒钟的毫秒数long diff;try {
        //获得两个时间的毫秒时间差异
        long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
        long day = diff / nd;//计算差多少天
        long hour = diff / nh;//计算差多少小时
        //long min = diff%nd%nh/nm;//计算差多少分钟
        //long sec = diff%nd%nh%nm/ns;//计算差多少秒//输出结果
        if (type.equals("1")) {
            return day;
        } else {
            return hour;
        }
    }

    /**
     * 计算日期距离当前时间 X天X小时X分钟  传递14日期格式
     *
     * @param dateTime
     * @return
     * @throws ParseException
     */
    public static String dateDiff(String dateTime) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String twoDate = sdf.format(new Date());
        Date d1 = sdf.parse(dateTime);
        Date d2 = sdf.parse(twoDate);
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = d2.getTime() - d1.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        //System.out.println(day + "天" + hour + "小时" + min + "分钟");
        if (day > 0) {
            return day + "天前";
        } else if (hour > 0) {
            return hour + "小时前";
        } else if (min > 0) {
            return min + "分钟前";
        } else {
            return "刚刚";
        }
    }

    //获取当前时间    yyyyMMdd
    public static String simpleDateOne() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN_YYYYMMDD_01);
        return dateFormat.format(date);
    }

    //获取当前时间  HHmmss
    public static String simpleTime() {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");//时:分:秒:毫秒-HH:mm:ss.SSS
        return sdf.format(d);
    }

    //获取当前月日
    public static String simpleMMdd() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("MMdd");
        return dateFormat.format(date);
    }

    //格式化日期14位
    public static String simpleDateFormat(String dateTime) {
        if (dateTime == null || dateTime.trim().equals("")) {
            return "";
        }
        ;
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = df.parse(dateTime);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return time.format(date);
    }

    //格式化日期14位--转为数字  yyyy-MM-dd HH:mm:ss -->yyyyMMddHHmmss
    public static String simpleDateFormatThree(String dateTime) {
        if (dateTime == null || dateTime.trim().equals("")) {
            return "";
        }
        ;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat time = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = null;
        try {
            date = df.parse(dateTime);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return time.format(date);
    }

    //格式化日期  yyyy-MM-dd HH:mm:ss -->yyyy/MM/dd HH:mm:ss
    public static String simpleDateFormatTwo(String dateTime) {
        if (dateTime == null || dateTime.trim().equals("")) {
            return "";
        }
        ;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat time = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = null;
        try {
            date = df.parse(dateTime);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return time.format(date);
    }

    //格式化日期格式8位  yyyyMMdd---------->yyyy-MM-dd
    public static String simpleDateFormatOne(String dateTime) {
        if (dateTime == null) {
            return "";
        }
        SimpleDateFormat sdf1 = new SimpleDateFormat(DATE_PATTERN_YYYYMMDD_01);
        SimpleDateFormat sdf2 = new SimpleDateFormat(DATE_PATTERN_YYYYMMDD_02);
        try {
            return sdf2.format(sdf1.parse(dateTime));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    //格式化日期格式  hhmmss---------->HH:mm:ss
    public static String simpleTimeFormatOne(String dateTime) {
        SimpleDateFormat sdf1 = new SimpleDateFormat(TIME_PATTERN_HHMMSS_01);
        SimpleDateFormat sdf2 = new SimpleDateFormat(TIME_PATTERN_HHMMSS_02);
        try {
            return sdf2.format(sdf1.parse(dateTime));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    //格式化日期格式  HH:mm:ss---------->hhmmss
    public static String simpleTimeFormatTwo(String dateTime) {
        SimpleDateFormat sdf1 = new SimpleDateFormat(TIME_PATTERN_HHMMSS_02);
        SimpleDateFormat sdf2 = new SimpleDateFormat(TIME_PATTERN_HHMMSS_01);
        try {
            return sdf2.format(sdf1.parse(dateTime));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    //去除日期的-（yyyy-mm-DD ----------------->YYYYMMDD）
    public static String disponseDate(String date) {
        date = date.replace("-", "");
        return date;
    }

    //格式化日期12位
    public static String simpleDateFormat12(String dateTime) {
        if (dateTime == null || dateTime.trim().equals("")) {
            return "";
        }
        ;
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
        SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        try {
            date = df.parse(dateTime);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return time.format(date);
    }

    /**
     * 判断长度进行格式化日期
     *
     * @param date
     * @return
     */
    public static String simpleDate(String date) {
        String newDate = "";
        if (date == null || date.trim().equals("")) {
            return newDate;
        }
        if (date.length() == 8) {
            newDate = simpleDateFormatOne(date);
        }
        if (date.length() == 12) {
            newDate = simpleDateFormat12(date);
        }
        if (date.length() == 14) {
            newDate = simpleDateFormat(date);
        }
        return newDate;
    }

    public static String formatDate(String formater) {
        SimpleDateFormat sdf = new SimpleDateFormat(formater);
        String date = sdf.format(new Date(System.currentTimeMillis()));
        return date;
    }

    /**
     * 格式化日期
     */
    public static String dateFor(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(date);
    }

    /**
     * 获取当前系统时间 14位
     * @return
     */
    public static String currentDateTime(){
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        return df.format(new Date());
    }


}
